home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue30 / expreval / EXPREVAL.ZIP / testform.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-01-02  |  5.9 KB  |  227 lines

  1. unit testform;
  2. interface
  3. {this unit implements the examples used to illustrate the
  4. use of the 'Expressions' unit. These examples are documented
  5. in Expressions.Pas. Please go refer to the main comment block
  6. (just before implementation) in that unit}
  7. uses
  8.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  9.   StdCtrls, Expressions, ExtCtrls;
  10.  
  11. type
  12.   TForm1 = class(TForm)
  13.     ICount: TLabel;
  14.     Label2: TLabel;
  15.     EG1Button: TButton;
  16.     EG2Button: TButton;
  17.     EG3Button: TButton;
  18.     EG3Result: TLabel;
  19.     EG3Timer: TTimer;
  20.     EG4Button: TButton;
  21.     procedure EG1ButtonClick(Sender: TObject);
  22.     procedure EG2ButtonClick(Sender: TObject);
  23.     procedure FormDestroy(Sender: TObject);
  24.     procedure EG3TimerTimer(Sender: TObject);
  25.     procedure EG3ButtonClick(Sender: TObject);
  26.     procedure EG4ButtonClick(Sender: TObject);
  27.   private
  28.   public
  29.     EG3Expr: TExpression;
  30.     procedure CheckInstances;
  31.     function EG2IDFunc( const Identifier: String;
  32.                            ParameterList: TParameterList): TExpression;
  33.     function EG3IDFunc( const Identifier: String;
  34.                            ParameterList: TParameterList): TExpression;
  35.     function EG4IDFunc( const Identifier: String;
  36.                            ParameterList: TParameterList): TExpression;
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.  
  42. implementation
  43. {$R *.DFM}
  44.  
  45. procedure TForm1.CheckInstances;
  46. begin
  47.   Icount.Caption:= IntToStr(InstanceCOunt)
  48. end;
  49.  
  50. procedure TForm1.EG1ButtonClick(Sender: TObject);
  51. var
  52.   s: String;
  53.   E: TExpression;
  54. begin
  55.   s:= '';
  56.   if InputQuery('Example 1', 'Enter an expression...', s) then
  57.   begin
  58.     E:= CreateExpression(s, nil);
  59.     if Assigned(E) then
  60.     try
  61.       MessageDlg(
  62.         Format('E.AsString = %s E.ExprType = %s',
  63.                [E.AsString, NExprType[E.ExprType]]),
  64.         mtInformation, [mbOK], 0)
  65.     finally
  66.       E.Free;
  67.       CheckInstances
  68.     end
  69.   end
  70. end;
  71.  
  72. function TForm1.EG2IDFunc( const Identifier: String;
  73.                               ParameterList: TParameterList): TExpression;
  74. {this identifier function is used for example 2. It supports the
  75. identifiers SC, FC, IC, and BC}
  76. begin
  77.   {these identifiers do not require parameters, so raise an exception if they exist.
  78.    Note that if a parameter list is passed (due to bad syntax) and then we return a
  79.    valid result, the parameter list will be orphaned and there will be a memory leak}
  80.   if Assigned(ParameterList) then
  81.     raise EExpression.CreateFmt('Identifier %s does not require parameters', [Identifier]);
  82.   if Identifier = 'SC' then
  83.     Result:= TStringLiteral.Create('This is a string')
  84.   else
  85.   if Identifier = 'FC' then
  86.     Result:= TFloatLiteral.Create(8.9)
  87.   else
  88.   if Identifier = 'IC' then
  89.     Result:= TIntegerLiteral.Create(42)
  90.   else
  91.   if Identifier = 'BC' then
  92.     Result:= TBooleanLiteral.Create(False)
  93.   else
  94.     Result:= nil
  95. end;
  96.  
  97. procedure TForm1.EG2ButtonClick(Sender: TObject);
  98. var
  99.   s: String;
  100.   E: TExpression;
  101. begin
  102.   s:= '';
  103.   if InputQuery('Example 2', 'Expression may contain' +
  104.      ' SC, FC, IC or BC', s) then
  105.   begin
  106.     E:= CreateExpression(s, EG2IDFunc);
  107.     if Assigned(E) then
  108.     try
  109.       MessageDlg(
  110.         Format('E.AsString = %s E.ExprType = %s',
  111.                [E.AsString, NExprType[E.ExprType]]),
  112.         mtInformation, [mbOK], 0)
  113.     finally
  114.       E.Free;
  115.       CheckInstances
  116.     end
  117.   end
  118. end;
  119.  
  120. type
  121.   TTimeString =
  122.   class(TExpression)
  123.   protected
  124.     function GetAsString: String; override;
  125.     function GetExprType: TExprType; override;
  126.   end;
  127.  
  128. function TTimeString.GetAsString: String;
  129. begin
  130.   Result:= FormatDateTime('hh:mm:ss', SysUtils.Time)
  131. end;
  132.  
  133. function TTimeString.GetExprType: TExprType;
  134. begin
  135.   Result:= ttString
  136. end;
  137.  
  138. function TForm1.EG3IDFunc( const Identifier: String;
  139.                               ParameterList: TParameterList): TExpression;
  140. {this identifier function is used for example 3. It supports the
  141. identifier TimeString}
  142. begin
  143.   if Assigned(ParameterList) then
  144.     raise EExpression.CreateFmt('Identifier %s does not require parameters', [Identifier]);
  145.   if Identifier = 'TIMESTRING' then
  146.     Result:= TTimeString.Create
  147.   else
  148.     Result:= nil
  149. end;
  150.  
  151. procedure TForm1.EG3TimerTimer(Sender: TObject);
  152. begin
  153.   if Assigned(EG3Expr) then
  154.     EG3Result.Caption:= EG3Expr.AsString
  155.   else
  156.     EG3Result.Caption:= 'EG3 not running'
  157. end;
  158.  
  159. procedure TForm1.FormDestroy(Sender: TObject);
  160. begin
  161.   EG3Expr.Free
  162. end;
  163.  
  164. procedure TForm1.EG3ButtonClick(Sender: TObject);
  165. var
  166.   s: String;
  167. begin
  168.   s:= 'TimeString';
  169.   if InputQuery('Example 3', 'Expression may contain' +
  170.      ' TimeString', s) then
  171.   begin
  172.     EG3Expr.Free;
  173.     EG3Expr:= CreateExpression(s, EG3IDFunc);
  174.     EG3Result.Caption:= EG3Expr.AsString;
  175.     CheckInstances
  176.   end
  177. end;
  178.  
  179. function TForm1.EG4IDFunc( const Identifier: String;
  180.                               ParameterList: TParameterList): TExpression;
  181. {this identifier function is used for example 4. It supports the
  182. function Mean(a, b: Float): Float; }
  183. begin
  184.   if Identifier = 'MEAN' then
  185.   begin
  186.     if Assigned(ParameterList) and
  187.        (ParameterList.Count = 2) then
  188.     begin
  189.       with ParameterList do
  190.         Result:= TFloatLiteral.Create((AsFloat[0] + AsFloat[1])/2);
  191.       ParameterList.Free
  192.     end else
  193.     begin
  194.       raise EExpression.CreateFmt('Invalid Parameters to %s', [Identifier]);
  195.     end;
  196.   end else
  197.   begin
  198.     Result:= nil
  199.   end
  200. end;
  201.  
  202.  
  203. procedure TForm1.EG4ButtonClick(Sender: TObject);
  204. var
  205.   s: String;
  206.   E: TExpression;
  207. begin
  208.   s:= '';
  209.   if InputQuery('Example 4', 'Expression may contain ' +
  210.      'Mean(a, b: Float)', s) then
  211.   begin
  212.     E:= CreateExpression(s, EG4IDFunc);
  213.     if Assigned(E) then
  214.     try
  215.       MessageDlg(
  216.         Format('E.AsString = %s E.ExprType = %s',
  217.                [E.AsString, NExprType[E.ExprType]]),
  218.         mtInformation, [mbOK], 0)
  219.     finally
  220.       E.Free;
  221.       CheckInstances
  222.     end
  223.   end
  224. end;
  225.  
  226. end.
  227.